<!DOCTYPE html>
<html lang="en">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="My Personal Website For Blog">
  <meta name="author" content="黄宇辉">
  <meta name="keywords" content="learning note,hexo blog">
  <title>Shiro的拦截器机制 - 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/vs2015.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">

<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>ANONYMOUS</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                Home
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                Archives
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                Tags
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                About
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="view intro-2" id="background" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2019-08-03 08:01">
      August 3, 2019 am
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      808 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      11
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
            <article class="markdown-body">
              <h2 id="学习笔记-Shiro的拦截机制"><a href="#学习笔记-Shiro的拦截机制" class="headerlink" title="学习笔记 : Shiro的拦截机制"></a>学习笔记 : Shiro的拦截机制</h2><h3 id="默认拦截器"><a href="#默认拦截器" class="headerlink" title="默认拦截器"></a>默认拦截器</h3><p><em>Shiro内置了很多默认的拦截器,比如身份验证、授权等相关的.更多默认拦截器可以参考 <code>org.apache.shiro.web.filter.mgt.DefaultFilter</code>中的枚举拦截器,如下所示 :</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-comment">/**
 * Enum representing all of the default Shiro Filter instances available to web applications.  
 * Each filter instance is typically accessible 
 * in configuration the &#123;<span class="hljs-doctag">@link</span> #name() name&#125; of the enum constant.
 *
 * <span class="hljs-doctag">@since</span> 1.0
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span> DefaultFilter &#123;

    anon(AnonymousFilter<span class="hljs-class">.<span class="hljs-keyword">class</span>),
    <span class="hljs-title">authc</span>(<span class="hljs-title">FormAuthenticationFilter</span>.<span class="hljs-title">class</span>),
    <span class="hljs-title">authcBasic</span>(<span class="hljs-title">BasicHttpAuthenticationFilter</span>.<span class="hljs-title">class</span>),
    <span class="hljs-title">logout</span>(<span class="hljs-title">LogoutFilter</span>.<span class="hljs-title">class</span>),
    <span class="hljs-title">noSessionCreation</span>(<span class="hljs-title">NoSessionCreationFilter</span>.<span class="hljs-title">class</span>),
    <span class="hljs-title">perms</span>(<span class="hljs-title">PermissionsAuthorizationFilter</span>.<span class="hljs-title">class</span>),
    <span class="hljs-title">port</span>(<span class="hljs-title">PortFilter</span>.<span class="hljs-title">class</span>),
    <span class="hljs-title">rest</span>(<span class="hljs-title">HttpMethodPermissionFilter</span>.<span class="hljs-title">class</span>),
    <span class="hljs-title">roles</span>(<span class="hljs-title">RolesAuthorizationFilter</span>.<span class="hljs-title">class</span>),
    <span class="hljs-title">ssl</span>(<span class="hljs-title">SslFilter</span>.<span class="hljs-title">class</span>),
    <span class="hljs-title">user</span>(<span class="hljs-title">UserFilter</span>.<span class="hljs-title">class</span>)</span>;

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> Class&lt;? extends Filter&gt; filterClass;

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">DefaultFilter</span><span class="hljs-params">(Class&lt;? extends Filter&gt; filterClass)</span> </span>&#123;
        <span class="hljs-keyword">this</span>.filterClass = filterClass;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> Filter <span class="hljs-title">newInstance</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> (Filter) ClassUtils.newInstance(<span class="hljs-keyword">this</span>.filterClass);
    &#125;

    <span class="hljs-keyword">public</span> Class&lt;? extends Filter&gt; getFilterClass() &#123;
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.filterClass;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Map&lt;String, Filter&gt; <span class="hljs-title">createInstanceMap</span><span class="hljs-params">(FilterConfig config)</span> </span>&#123;
        Map&lt;String, Filter&gt; filters = <span class="hljs-keyword">new</span> LinkedHashMap&lt;String, Filter&gt;(values().length);
        <span class="hljs-keyword">for</span> (DefaultFilter defaultFilter : values()) &#123;
            Filter filter = defaultFilter.newInstance();
            <span class="hljs-keyword">if</span> (config != <span class="hljs-keyword">null</span>) &#123;
                <span class="hljs-keyword">try</span> &#123;
                    filter.init(config);
                &#125; <span class="hljs-keyword">catch</span> (ServletException e) &#123;
                    String msg = <span class="hljs-string">"Unable to correctly init default filter instance of type "</span> +
                            filter.getClass().getName();
                    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalStateException(msg, e);
                &#125;
            &#125;
            filters.put(defaultFilter.name(), filter);
        &#125;
        <span class="hljs-keyword">return</span> filters;
    &#125;
&#125;</code></pre></div>

<p><img src="/2019/08/03/Shiro%E7%9A%84%E6%8B%A6%E6%88%AA%E5%99%A8%E6%9C%BA%E5%88%B6/Shiro-ShiroFilter.jpg" srcset="/img/loading.gif" alt></p>
<table>
<thead>
<tr>
<th align="center">过滤器简称</th>
<th align="center">对应的java类</th>
</tr>
</thead>
<tbody><tr>
<td align="center">anon</td>
<td align="center">org.apache.shiro.web.filter.authc.AnonymousFilter</td>
</tr>
<tr>
<td align="center">authc</td>
<td align="center">org.apache.shiro.web.filter.authc.FormAuthenticationFilter</td>
</tr>
<tr>
<td align="center">authcBasic</td>
<td align="center">org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter</td>
</tr>
<tr>
<td align="center">roles</td>
<td align="center">org.apache.shiro.web.filter.authz.RolesAuthorizationFilter</td>
</tr>
<tr>
<td align="center">perms</td>
<td align="center">org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter</td>
</tr>
<tr>
<td align="center">user</td>
<td align="center">org.apache.shiro.web.filter.authc.UserFilter</td>
</tr>
<tr>
<td align="center">logout</td>
<td align="center">org.apache.shiro.web.filter.authc.LogoutFilter</td>
</tr>
<tr>
<td align="center">port</td>
<td align="center">org.apache.shiro.web.filter.authz.PortFilter</td>
</tr>
<tr>
<td align="center">rest</td>
<td align="center">org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter</td>
</tr>
<tr>
<td align="center">ssl</td>
<td align="center">org.apache.shiro.web.filter.authz.SslFilter</td>
</tr>
</tbody></table>
<ol>
<li><code>anon</code> : 匿名拦截器,即不需要登录即可访问. 一般用于静态资源过滤,示例”/static/**=anon”</li>
<li><code>authc</code> : 表示需要认证(登录)才能使用,示例”/**=authc”,主要属性有 : <ul>
<li>usernameParam : 表单提交的用户名参数名(username)</li>
<li>passwordParam : 表单提交的密码参数名(password)</li>
<li>rememberMeParam : 表单提交的密码参数名(rememberMe)</li>
<li>loginUrl : 登录请求地址(/login.jsp)</li>
<li>successUrl : 登录成功后的默认重定向地址</li>
<li>failureKeyAttribute : 登录失败后错误信息存储key(shiroLoginFailure)</li>
</ul>
</li>
<li><code>authcBasic</code> : Basic HTTP身份验证拦截器,主要属性 : applicationName:弹出登录框显示的信息(application)</li>
<li><code>roles</code> : 角色授权拦截器,验证用户是否拥有资源角色. 示例”/admin/**=roles[admin]”</li>
<li><code>perms</code> : 权限授权拦截器,验证用户是否拥有资源权限. 示例”/user/create=perms[“user:create”]”</li>
<li><code>user</code> : 用户拦截器,用户已经身份验证/记住我的登录. 示例”/index=user”</li>
<li><code>logout</code> : 退出拦截器,主要属性 : redirectUrl:退出成功后重定向的地址(/). 示例”/logout=logout”</li>
<li><code>port</code> : 端口拦截器,主要属性 : port(80):可以通过的端口. 示例”/test= port[80]”,如果用户访问该页面是非80,将自动将请求端口改为80并重定向到该80端口,其他路径/参数等相同</li>
<li><code>rest</code> : rest风格拦截器,自动根据请求方法构建权限字符串(GET=read,POST=create,PUT=update,DELETE=delete,HEAD=read,TRACE=read,OPTIONS=read, MKCOL=create)构建权限字符串. 示例”/users=rest[user]”,会自动拼出”user:read,user:create,user:update,user:delete”权限字符串进行权限匹配(所有都得匹配:isPermittedAll)</li>
<li><code>ssl</code> : SSL拦截器,只有请求协议是https才能通过. 否则自动跳转会https端口(443). 其他和port拦截器一样</li>
</ol>
<ul>
<li>注 : anon,authcBasic,auchc,user是认证过滤器. perms,roles,ssl,rest,port是授权过滤器</li>
</ul>

            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Java/">Java</a>
                    
                      <a class="hover-with-bg" href="/tags/Shiro/">Shiro</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/2019/08/04/Hi-Layui/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Hi Layui ~</span>
                        <span class="visible-mobile">Previous</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/2019/08/02/Shiro%E4%B9%8B%E7%94%A8%E6%88%B7%E6%8E%88%E6%9D%83/">
                        <span class="hidden-mobile">Shiro之用户授权</span>
                        <span class="visible-mobile">Next</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;TOC</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Search</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">keyword</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "Shiro的拦截器机制&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  














</body>
</html>
